package com.jsb.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.jsb.common.config.Const;
import com.jsb.model.Question;
import com.jsb.model.QuestionExample;
import com.jsb.model.QuestionExample.Criteria;

@Service
public class QuartzService {
	private static final Logger log= Logger.getLogger(QuartzService.class);
	
	@Resource
	private QuestionService questionService;
	/**
	 * 结束悬赏定时任务
	 */
	public void updateReward(){
		log.info("开始结束悬赏定时任务");
		QuestionExample example = new QuestionExample();
		Criteria criteria = example.createCriteria();
		Date now = new Date();
		criteria.andStatusEqualTo(Integer.valueOf(Const.QUESTIONINIT));
		criteria.andEndDateLessThan(now);
		List<Question> questions = questionService.selectByExample(example);
		log.info("待结束的悬赏任务个数："+questions.size());
		if(null == questions || questions.size()==0){
			return;
		}
		for (Question question : questions) {
			String uuid = question.getUuid();
			Question question2 = new Question();
			question2.setUuid(uuid);
			question2.setStatus(Integer.valueOf(Const.QUESTIONSTOP));
			questionService.updateQuestion(question2);               //结束悬赏
			log.info("结束悬赏问题："+uuid);
			questionService.updateReward(question);               //分发悬赏
		}
	}
}
